<?php

namespace App\Admin\Forms\Tenant;

use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class MainSiteMetaForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        Settings::set('main_site_admin_logo', $input['main_site_admin_logo'], true);
        Settings::set('main_site_admin_name', trim_all_space($input['main_site_admin_name']), true);
        Settings::set('main_site_admin_avatar', $input['main_site_admin_avatar'], true);

        Settings::set('main_site_admin_site', $input['main_site_admin_site'], true);
        Settings::set('main_site_admin_agent', $input['main_site_admin_agent'], true);
        Settings::set('main_site_admin_customized', $input['main_site_admin_customized'], true);
        Settings::set('main_site_admin_doc', $input['main_site_admin_doc'], true);

        Settings::set('main_site_admin_admin_name', $input['main_site_admin_admin_name'], true);
        Settings::set('main_site_admin_admin_password', $input['main_site_admin_admin_password'], true);

        Settings::set('main_site_admin_login_bg', $input['main_site_admin_login_bg'], true);
        Settings::set('main_site_bt_address', $input['main_site_bt_address'], true);


        _update_env([
            'MAIN_SITE_ADMIN_LOGO' => $input['main_site_admin_logo'],
            'MAIN_SITE_ADMIN_NAME' => trim_all_space($input['main_site_admin_name']),
            'MAIN_SITE_ADMIN_AVATAR' => $input['main_site_admin_avatar'],
        ], [], 1);

        return $this->response()
            ->success('配置成功')
            ->refresh();
    }

    public function form()
    {
        $this->image('main_site_admin_logo', 'LOGO')->help('<a href="https://mini.minisns.cn/statics/minisns.svg" target="_blank">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('main_site_admin_logo', 'https://mini.minisns.cn/statics/minisns.svg'));
        $this->image('main_site_admin_avatar', '小LOGO')->help('<a href="https://mini.minisns.cn/statics/mlogo.svg" target="_blank">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('main_site_admin_avatar', 'https://mini.minisns.cn/statics/mlogo.svg'));
        $this->text('main_site_admin_name', '品牌名')->default(Settings::get('main_site_admin_name', 'MiniSNS'));

        $this->text('main_site_admin_site', '官网')->default(Settings::get('main_site_admin_site', 'https://www.minisns.cn'));
        $this->url('main_site_admin_agent', '代理商')->default(Settings::get('main_site_admin_agent', 'https://doc.minisns.cn/doc/99/'));
        $this->url('main_site_admin_customized', '定制服务')->default(Settings::get('main_site_admin_customized', 'https://doc.minisns.cn/doc/132/'));
        $this->url('main_site_admin_doc', '文档地址')->default(Settings::get('main_site_admin_doc', 'https://doc.minisns.cn'));

        $this->text('main_site_admin_admin_name', '默认用户名')->help('实际默认用户名统一为：admin，这里的意思是后台登录页面自动铺满的用户名')->default(Settings::get('main_site_admin_admin_name', ''));
        $this->text('main_site_admin_admin_password', '默认密码')->help('实际登录密码默认统一为：minisns666，这里的意思是登录页面自动铺满的密码')->default(Settings::get('main_site_admin_admin_password', ''));

        $this->image('main_site_admin_login_bg', '登录背景')->help('<a href="https://img.mini.minisns.cn/back/back.jpg" target="_blank">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('main_site_admin_login_bg', 'https://img.mini.minisns.cn/back/back.jpg'));

        $this->text('main_site_bt_address', '宝塔地址')->help('<a href="https://img.mini.minisns.cn/back/version_tip.png" target="_blank">查看说明</a>')->default(Settings::get('main_site_bt_address', 'https://doc.minisns.cn/doc/112/'));

    }
}
